<?php
class IndexAction extends CommonAction
{
    public function index()
    {
    	$userinfo = $this->getLoginUserInfo();
    	$roleinfo = $this->getUserRole();
    	$NavG = $this->getManageNavG($roleinfo[0]);
    	$TopNav = $this->getTopNav($NavG);
    	$menu = $this->getMenuList($NavG);
    	$this->assign('loginuser',$userinfo['account']);
		$this->assign('menu',$menu);
    	$this->assign('TopNav',$TopNav);
        $this->display();
    }
	
    /**
     * +-------------------------------------------------------
     * @ 首页内容页
     * +-------------------------------------------------------
     */
    public function main()
    {

    	$this->display();
    }
    
	/**
     * +-------------------------------------------------------
     * @ 后台登陆
     * +-------------------------------------------------------
     */
    public function login()
    {
    	if (isset($_POST['user']) && !empty($_POST['user'])) $this->checklogin();
 		if ($_SESSION[C('USER_AUTH_KEY')])	$this->redirect('Index/index');
		$this->display();
    }
    
	/**
     * +-------------------------------------------------------
     * @ 后台登陆
     * +-------------------------------------------------------
     */
    public function logout()
    {
    	if(isset($_SESSION[C('USER_AUTH_KEY')])) {				
			unset($_SESSION[C('USER_AUTH_KEY')]);
			unset($_SESSION);
			session_destroy();
            $this->assign("jumpUrl",__URL__.'/login/');
            $this->success('登出成功！');
        }else {
        	$this->assign("jumpUrl",__URL__.'/login/');
            $this->error('已经登出！');
        }
    }
    
	/**
     * +-------------------------------------------------------
     * @ 验证码
     * +-------------------------------------------------------
     */
    public function verify()
    {
		$type=isset($_GET['type'])?$_GET['type']:'gif';
 		import("ORG.Util.Image");
 		Image::buildImageVerify(4,1,$type);
    }
	
    /**
     * +-------------------------------------------------------
     * @ 检查登录
     * +------------------------------------------------------- 
     */
    private function checklogin()
    {
    	if (empty($_POST['password'])) $this->error('请填写密码,密码区分大小写');
    	if (empty($_POST['vcode'])) $this->error(L('_VCODE_ERR'));
    	if (md5(strtolower($_POST['vcode'])) != $_SESSION['verify']) $this->error(L('_VCODE_ERR'));
    	
    	$map['account'] = array('eq',strtolower(trim($_POST['user'])));
    	import('@.ORG.IIARBAC');
		$Find_User = IIARBAC::LoginAuthenticaate($map);
		if (empty($Find_User)) $this->error(L('_USER_NAME_ERR'));
		if ($Find_User['status'] != 1) $this->error(L('_USER_DISABLE'));
		if ($Find_User['verify'] != 1) $this->error(L('_USER_NO_VERIFY'));
		
		if ($Find_User['err_count'] >= 5)
		{
			$nowtime = time();
			$overlocktime = round(($nowtime - $Find_User['err_login_time'])/3600,1);
			$shenyuTime = C('LOCKED_IP_TIME') - $overlocktime;
			if ($overlocktime < C('LOCKED_IP_TIME')) $this->error('你的账户现为冻结期，请'.$shenyuTime.'小时之后登录');
		}
		
		
		$UForm = M(C('USER_AUTH_MODEL'));
		$ip	= get_client_ip();
		$LoginTime = time();
        $data = array();
		$data['id']	= $Find_User['id'];
		
		$logPass = trim($_POST['password']);
		if ($Find_User['password'] != pwdHash($logPass))
		{
			$data['err_login_time'] = $LoginTime;
			$data['err_login_ip'] = $ip;
			$data['err_count'] = array('exp','err_count+1');							// 登录错误次数加一
			
			$residueErrNum = C('ERR_LOG_MAXNUM')-$Find_User['err_count']-1;
			if ($residueErrNum > 0) 
			{										
				$errmsg = '密码与账户不想符，你还有 '.$residueErrNum.' 次尝试登录机会';
			}
			if ($residueErrNum == 0)
			{
				$data['status'] = 0;
				$errmsg = '因多次尝试登录失败，你的账户被被禁用。请'.C('LOCKED_IP_TIME').'小时之后重新登录';
			}
			$UForm->save($data);
			$this->error($errmsg);
		}
		
		$_SESSION[C('USER_AUTH_KEY')] = $Find_User['id'];
		$_SESSION[C('USER_POPEDOM_KEY')]['LoginTime'] = $LoginTime;
		$_SESSION[C('USER_POPEDOM_KEY')]['autmstrfo'] = pwdHash($Find_User['account'].$Find_User['reg_time'].$Find_User['uname'].$Find_User['update_time'].$Find_User['password'].$ip.$Find_User['err_login_time']);
		$_SESSION[C('USER_POPEDOM_KEY')]['last_login_ip'] = $Find_User['last_login_ip'];
		$_SESSION[C('USER_POPEDOM_KEY')]['last_login_time'] = $Find_User['last_login_time'];
		
		$data['last_login_time'] = $LoginTime;
		$data['last_login_ip'] = $ip;
		$data['login_counts'] = array('exp','login_count +1');
		$data['err_count'] = 0;
		
		$UForm->save($data);
		IIARBAC::saveAccessList();
		$this->success('<b>'.$Find_User['account'].'</b>，'.L('_LOGIN_OK'));
    }
    /**
     * +-------------------------------------------------------
     * @ 获取授权给当前登录用户的菜单
     * +-------------------------------------------------------
     */
    private function getMenuList($NavG)
    {
    	$authid = $_SESSION[C('USER_AUTH_KEY')];
    	import('@.ORG.IIARBAC');
    	$acc = IIARBAC::getUesrNodeList($authid);
    	$map['nav_id'] = array('in',$NavG);
    	$map['id'] = array('in',$acc);
    	$map['status'] = array('eq',1);
    	
    	$list = M(C('RBAC_NODE_TABLE'))->where($map)->order('sort asc, id asc')->select();
    	
    	$menu = array();
    	foreach ($list as $mval)
    	{
    		if($mval['level'] == 1) $menu[$mval['nav_id']]['mod'][$mval['id']] = array('id'=>$mval['id'],'ename'=>$mval['ename'],'name'=>$mval['name']);
    		if($mval['level'] == 2) $menu[$mval['nav_id']]['act'][$mval['pid']][$mval['ename']] = $mval['name'];
    	}
    	
    	return $menu;
    	
    }
    
    /**
     * +-------------------------------------------------------
     * @ 获取当前用户授权的菜单组
     * +-------------------------------------------------------
     */
    private function getTopNav($rnav)
    {
    	$Form = M('Manav');
    	$map['status'] = array('eq',1);
    	$map['id'] = array('in',$rnav);
    	$list = $Form->where($map)->getField('id,name');
    	return $list;
    }
}
?>